﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using Do_an_C_sharp.DAL;

namespace Do_an_C_sharp.BLL
{
    class KhachHangController
    {
        DemoDataContext db = new DemoDataContext();
        public List<KhachHang> laykhach()
        {
            return db.KhachHangs.ToList();
        }

        public List<KhachHang> timma(string ma)
        {
            return db.KhachHangs.Where(kh=> kh.MAKHACHHANG == ma).ToList();
        }
        public List<KhachHang> timten(string ten)
        {
            return db.KhachHangs.Where(kh => kh.TENKHACHHANG.Contains(ten)).ToList();
        }
        public KhachHang timelement(string ma)
        {
            return db.KhachHangs.Where(kh => kh.MAKHACHHANG == ma).FirstOrDefault();
        }
        public void them(string makhach,string ten,string diachi,string dienthoai,string email)
        {
            //xây dựng chưa hoàn thiện
            KhachHang khach = new KhachHang();
            khach.MAKHACHHANG = makhach;
            khach.TENKHACHHANG = ten;
            khach.DIACHI = diachi;
            khach.DIENTHOAI = dienthoai;
            khach.EMAIL = email;
            db.KhachHangs.InsertOnSubmit(khach);
        }
        public void xoa(string ma)
        {
            db.KhachHangs.DeleteOnSubmit(timelement(ma));
            //db.SubmitChanges();
        }
        public bool sua(string ma, string ten, string diachi, string dienthoai, string email)
        {
            //mới xây dựng để sửa mô tả thôi
            KhachHang h = timelement(ma);
            if (h != null)
            {
                h.TENKHACHHANG = ten;
                h.DIACHI = diachi;
                h.DIENTHOAI = dienthoai;
                h.EMAIL = email;
                return true;
            }
            return false;
        }
        public void luu()
        {
            db.SubmitChanges();
        }
    }
}
